/*
 * protocol.cpp
 *
 *  Created on: Mar 31, 2012
 *      Author: Victor Nidens victornlj@gmail.com
 *      Author:	Mikhail S. Ananyevskiy, PhD, msaipme@yandex.ru
 *
 *	This file is part of Cloud Mechatronic Laboratory (CMLab) software.
 *
 *	Cloud Mechatronic Laboratory (CMLab) for CMLab is free software: you can redistribute it and/or modify
 *	it under the terms of the GNU Lesser General Public License as published by
 *	the Free Software Foundation, either version 3 of the License, or
 *	(at your option) any later version.
 *
 *	Cloud Mechatronic Laboratory (CMLab) software is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU Lesser General Public License for more details.
 *
 *	You should have received a copy of the GNU Lesser General Public License
 *	along with Cloud Mechatronic Laboratory (CMLab). If not, see <http://www.gnu.org/licenses/>.
 */

#include "protocol.h"

void fillSourceInfo(Source *dst, uint8* sourceIP, string sourceName, uint8 protocol) {

	if(!dst)
		return;

	struct timeval timestamp;

	memcpy(dst->sourceName, sourceName.c_str(), sourceName.size() > sizeof(dst->sourceName) ? sizeof(dst->sourceName) : sourceName.size());

	memcpy(dst->sourceIp, sourceIP, sizeof(dst->sourceIp));

	gettimeofday(&timestamp, NULL);

	dst->timestampSec = timestamp.tv_sec;
	dst->timestampUsec = timestamp.tv_usec;

	dst->sourceProtocol = protocol;
}

void fillSourceInfo(Source *dst, Source *src){
	if(!dst || !src)
		return;

	struct timeval timestamp;

	memcpy(dst, src, sizeof(Source));

	gettimeofday(&timestamp, NULL);

	dst->timestampSec = timestamp.tv_sec;
	dst->timestampUsec = timestamp.tv_usec;

}

void constructCommand(Command *command, uint8 commandId, char *dstName, uint16 repeats, uint16 delay, uint8 responseProtocol){

	if(!command)
		return;

	command->command = commandId;
	command->repeatsNumber = repeats;
	command->repeatDelay = delay;
	command->responseProtocol = responseProtocol;

	if(dstName){
		memcpy(command->dst, dstName, sizeof(command->dst) > strlen(dstName) ? strlen(dstName) : sizeof(command->dst));
	}

}

char* commandName(uint8 commandId){
	switch(commandId){
	case COMMAND_PING:
		return "PING";
	case COMMAND_DEVICE_ANNOUNCE:
		return "DEVICE ANNOUNCE";
	case COMMAND_DEVICE_ANNOUNCE_RESPONSE:
		return "DEVICE ANNOUNCE RESPONSE";
	case COMMAND_READ_MOTOR_REQUEST:
		return "READ MOTOR REQUEST";
	case COMMAND_READ_MOTOR_RESPONSE:
		return "READ MOTOR RESPONSE";
	case COMMAND_READ_SENSOR_REQUEST:
		return "READ SENSOR REQUEST";
	case COMMAND_READ_SENSOR_RESPONSE:
		return "READ SENSOR RESPONSE";
	case COMMAND_SYSTEM_REQUEST:
		return "SYSTEM REQUEST";
	case COMMAND_SYSTEM_RESPONSE:
		return "SYSTEM RESPOSNE";
	case COMMAND_WRITE_MOTOR_REQUEST:
		return "WRITE MOTOR REQUEST";
	case COMMAND_WRITE_MOTOR_RESPONSE:
		return "WRITE MOTOR RESPONSE";
	case COMMAND_WRITE_SENSOR_REQUEST:
		return "WRITE SENSOR REQUEST";
	case COMMAND_WRITE_SENSOR_RESPONSE:
		return "WRITE SENSOR RESPONSE";
	default:
		return "";
	}
}

